package com.seehack.sample.thread.concurrent;

import java.util.concurrent.CountDownLatch;

/**
 *
 *  CountDownLatch使用：
 *  1.声明计数对象实例
 *  2.在并发线程中进行计数递减
 *  3.在需要同步的位置进行等待
 *
 */
public class CountDownLatchSample {

    public static void main(String[] args) throws InterruptedException {
        int count = 5;
        CountDownLatch countDownLatch = new CountDownLatch(count);
        for (int i = 0; i < count; i++) {
            final int index = i;
            new Thread(()->{
                System.out.println("index:"+index);
                countDownLatch.countDown();
            }).start();
        }
        // 等待计数器减少到0执行以后逻辑
        countDownLatch.await();
        System.out.println("CountDownLatch end.");
    }

}
